package com.test.springcloudservice.util;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@NoArgsConstructor
@ToString
public class ResponseData implements Serializable {

    private static final long serialVersionUID = -1038827901225739208L;

    /**
     * 状态码：0-成功，1-失败
     * */
    private Boolean status;

    /**
     * 错误消息，如果成功可为空或SUCCESS
     * */
    private String msg;

    /**
     * 返回结果数据
     * */
    private Object data;

    /**
     * 一共多少页
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Long page;

    /**
     * 当前页
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Long current;

    /**
     * 每页大小
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Long pageSize;

    /**
     * 数据一共多少条
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Long total;

    public static ResponseData success() {
        return success(null);
    }

    public static ResponseData success(Object data) {
        ResponseData result = new ResponseData();
        result.setStatus(true);
        result.setMsg("SUCCESS");
        result.setData(data);
        return result;
    }

    public static ResponseData successPage(Page page) {
        ResponseData result = new ResponseData();
        result.setStatus(true);
        result.setMsg("SUCCESS");
        result.setData(page.getRecords());
        result.setPage(page.getPages());
        result.setPageSize(page.getSize());
        result.setTotal(page.getTotal());
        result.setCurrent(page.getCurrent());
        return result;
    }

    public static ResponseData fail(String msg) {
        return fail(msg,null);
    }

    public static ResponseData fail(String msg, Object data) {
        ResponseData result = new ResponseData();
        result.setStatus(false);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
